package com.hangzhou.xyj.ui.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.DramaModel;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.model.type.EUserType;
import com.hangzhou.xyj.net.handler.ReportHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.ReportReq;
import com.hangzhou.xyj.ui.activity.DynamicDetailActivity;
import com.hangzhou.xyj.ui.activity.HisFocusActivity;
import com.hangzhou.xyj.ui.activity.MyFansActivity;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicImgViewHolder;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicListenViewHolder;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicTxtViewHolder;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.SelectMenuDialog;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.DensityUtil;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by neo.duan on 2015/12/28.
 * 信息主页adapter
 */
public class HomePageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private UserModel mUserInfo;//用户的基本信息
    private List<DynamicModel> mDynamicList;//用户的动态信息
    private List<DramaModel> mDramaModelList = new ArrayList<>();//传到播放器的List
    private Map<Integer, Integer> positionPosMap = new HashMap<>();//显示的列表的位置 与 List中的位置的映射
    private EUserType eUserType;

    public HomePageAdapter(Context context, UserModel userInfo, List<DynamicModel> dynamicList) {
        this.mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mUserInfo = userInfo;
        this.mDynamicList = dynamicList;
        eUserType = PublicUtil.getUserType(mUserInfo.getType(), mUserInfo.getCheckFlag());
        getDramaPlayList();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case 0: //107dp的一个透明View
                View view = new View(mContext);
                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        DensityUtil.dip2px(mContext, 107)));
                viewHolder = new RecyclerView.ViewHolder(view) {
                };
                break;
            case 1: //个人信息
                viewHolder = new UserInfoViewHolder(mLayoutInflater.inflate(R.layout.layout_home_page_person_info, viewGroup, false));
                break;
            case DynamicModel.TYPE_IMG:    //动态item
                viewHolder = new DynamicImgViewHolder(mContext, mLayoutInflater.inflate(R.layout.layout_active_item_img, viewGroup, false));
                break;
            case DynamicModel.TYPE_LISTEN:
                viewHolder = new DynamicListenViewHolder(mContext, mLayoutInflater.inflate(R.layout.layout_active_item_listen, viewGroup, false));
                break;
            case DynamicModel.TYPE_TEXT:
            default:
                viewHolder = new DynamicTxtViewHolder(mContext, mLayoutInflater.inflate(R.layout.layout_active_item_text, viewGroup, false));
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        final Intent intent;
        switch (getItemViewType(position)) {
            case 0:
                break;
            case 1:
                ((UserInfoViewHolder) viewHolder).setData(mUserInfo);
                ((UserInfoViewHolder) viewHolder).mCivHeadImg.setOnClickListener(this);
                ((UserInfoViewHolder) viewHolder).mLlFocus.setOnClickListener(this);
                ((UserInfoViewHolder) viewHolder).mLlFans.setOnClickListener(this);
                ((UserInfoViewHolder) viewHolder).mIvAuthen.setOnClickListener(this);
                break;
            case DynamicModel.TYPE_IMG:
                ((DynamicImgViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_PERSONAL_INFO.getValue());
                ((DynamicImgViewHolder) viewHolder).setData(mDynamicList.get(position - 2));
                intent = new Intent(mContext, DynamicDetailActivity.class);
                intent.putExtra(DataKey.TITLE, mDynamicList.get(position - 2).getUserName());
                intent.putExtra(DataKey.INDEX, mDynamicList.get(position - 2).getId());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
            case DynamicModel.TYPE_TEXT:
                ((DynamicTxtViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_PERSONAL_INFO.getValue());
                ((DynamicTxtViewHolder) viewHolder).setData(mDynamicList.get(position - 2));
                intent = new Intent(mContext, DynamicDetailActivity.class);
                intent.putExtra(DataKey.TITLE, mDynamicList.get(position - 2).getUserName());
                intent.putExtra(DataKey.INDEX, mDynamicList.get(position - 2).getId());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
            case DynamicModel.TYPE_LISTEN:
            default:
                ((DynamicListenViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_PERSONAL_INFO.getValue());
                ((DynamicListenViewHolder) viewHolder).setData(mDynamicList.get(position - 2));
//                final DynamicModel dynamicModel = mDynamicList.get(position - 2);
//
//                DramaModel dramaModel = new DramaModel();
//                dramaModel.setId(dynamicModel.getId());
//                dramaModel.setName(dynamicModel.getSongName());
//                dramaModel.setNativeUrl(dynamicModel.getNativeUrl());
//                dramaModel.setAccompanyUrl(dynamicModel.getAccompanyUrl());
//                dramaModel.setLrcUrl(dynamicModel.getLrcUrl());
//                mDramaModelList.add(dramaModel);
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntentManager.getInstance().goDramaPlayActivity(mContext, mDramaModelList, positionPosMap.get(position - 2));
                    }
                });
        }

    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
            case 1:
                return position;
            default:
                return mDynamicList.get(position - 2).getRealShowType();
        }
    }

    /**
     * 获取播放列表
     *
     * @return
     */
    private void getDramaPlayList() {
        mDramaModelList.clear();//先清空
        positionPosMap.clear();//先清空
        for (int i = 0; i < mDynamicList.size(); i++) {
            DynamicModel model = mDynamicList.get(i);
            if (model.getRealShowType() == DynamicModel.TYPE_LISTEN) {//如果是 试听类型的才加入
                DramaModel dramaModel = new DramaModel();
                dramaModel.setId(model.getId());
                dramaModel.setName(model.getSongName());
                dramaModel.setNativeUrl(model.getNativeUrl());
                dramaModel.setAccompanyUrl(model.getAccompanyUrl());
                dramaModel.setLrcUrl(model.getLrcUrl());
                mDramaModelList.add(dramaModel);
                positionPosMap.put(i, mDramaModelList.size() - 1);
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mDynamicList == null || mDynamicList.size() == 0) {
            return 2;//即使没有动态信息，也有2个
        }
        return mDynamicList.size() + 2;
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.civ_header:
                SelectMenuDialog menuDialog = new SelectMenuDialog(mContext);
                menuDialog.setMenu(new String[]{"举报该用户"}, new SelectMenuDialog.SelectMenuClickListener() {
                    @Override
                    public void onSelectMenuClick(Dialog dialog, View item, int position) {
                        dialog.dismiss();
                        //举报
                        doReport(mUserInfo.getUserId());
                    }
                });
                menuDialog.show();
                break;
            case R.id.ll_my_focus://跳转关注列表页面
                intent = new Intent(mContext, HisFocusActivity.class);
                intent.putExtra(DataKey.INDEX, mUserInfo.getUserId());
                if ("女".equals(mUserInfo.getSex())) {
                    intent.putExtra(DataKey.TITLE, "她的关注");
                } else {
                    intent.putExtra(DataKey.TITLE, "他的关注");
                }
                IntentManager.getInstance().goActivity(mContext, intent);
                break;
            case R.id.ll_my_fans:
                intent = new Intent(mContext, MyFansActivity.class);
                intent.putExtra(DataKey.INDEX, mUserInfo.getUserId());
                if ("女".equals(mUserInfo.getSex())) {
                    intent.putExtra(DataKey.TITLE, "她的粉丝");
                } else {
                    intent.putExtra(DataKey.TITLE, "他的粉丝");
                }
                IntentManager.getInstance().goActivity(mContext, intent);
                break;
            case R.id.miv_authen:
                switch (eUserType) {
                    case AUTHORIZED_TEAM://剧团
                        PublicUtil.getAuthenDramaGroupDialog(mContext, mUserInfo.getBelongOrg(),
                                mUserInfo.getOwner(), mUserInfo.getCatStr()).show();
                        break;
                    case AUTHORIZED_USER://演员
                        PublicUtil.getAuthenActorDialog(mContext, mUserInfo.getOwner(), mUserInfo.getTeacherFrom(),
                                mUserInfo.getCatStr(), mUserInfo.getBelongOrg());
                        break;
                    case FAMOUS_EXPERT://名家
                        PublicUtil.getAuthenExpertDialog(mContext, mUserInfo.getOwner(), mUserInfo.getTeacherFrom(),
                                mUserInfo.getCatStr());
                        break;
                }
                break;
        }
    }

    private void doReport(String userId) {
        ReportReq reportReq = new ReportReq(Integer.valueOf(userId), 3, Integer.valueOf(CommonPreferences.getUserId(mContext)));
        HttpLoader.getInstance().sendRequest(new ReportHandler(reportReq, new HttpResponseListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(Object model) {
                if ((Boolean) model) {
                    ToastUtil.show("举报成功！");
                }
            }

            @Override
            public void onFail(String msg) {

            }

            @Override
            public void onDone(String msg) {

            }

            @Override
            public void onError() {

            }
        }, 0));
    }

    /**
     * 用户基本信息ViewHolder
     */
    class UserInfoViewHolder extends RecyclerView.ViewHolder {
        CircleImageView mCivHeadImg;//头像
        MyImageView mIvAuthen;//认证图片
        MyImageView mIvGender;//性别图片
        MyTextView mTvNickName;//昵称
        MyTextView mTvSign;//签名
        MyTextView mTvDynamicNum;//动态数
        MyTextView mTvFocusNum;//关注数
        MyTextView mTvFansNum;//粉丝数
        LinearLayout mLlPersonalInfo;//个人信息
        LinearLayout mLlFocus;//关注
        LinearLayout mLlFans;//粉丝
        LinearLayout mLlNoContent;//没有动态的时候显示


        public UserInfoViewHolder(View itemView) {
            super(itemView);
            mCivHeadImg = (CircleImageView) itemView.findViewById(R.id.civ_header);
            mIvAuthen = (MyImageView) itemView.findViewById(R.id.miv_authen);
            mTvNickName = (MyTextView) itemView.findViewById(R.id.nick_name);
            mTvSign = (MyTextView) itemView.findViewById(R.id.mtv_sign);
            mTvDynamicNum = (MyTextView) itemView.findViewById(R.id.mtv_dynamic_num);
            mTvFocusNum = (MyTextView) itemView.findViewById(R.id.mtv_focus_num);
            mTvFansNum = (MyTextView) itemView.findViewById(R.id.mtv_fans_num);
            mLlPersonalInfo = (LinearLayout) itemView.findViewById(R.id.ll_personal_info);
            mLlFocus = (LinearLayout) itemView.findViewById(R.id.ll_my_focus);
            mLlFans = (LinearLayout) itemView.findViewById(R.id.ll_my_fans);
            mLlNoContent = (LinearLayout) itemView.findViewById(R.id.ll_no_content);
            mIvGender = (MyImageView) itemView.findViewById(R.id.gender);
        }

        public void setData(UserModel data) {
            mCivHeadImg.displayImage(data.getHeadPic());
            mIvAuthen.displayImage(PublicUtil.getAuthenIconStringUri(mContext, PublicUtil.getUserType(data.getType(), data.getCheckFlag())));
            mTvNickName.setText(data.getNickName());
            mTvSign.setText(data.getSignature());
            mTvDynamicNum.setText(String.valueOf(data.getDynamicNum()));
            mTvFocusNum.setText(String.valueOf(data.getFollowNum()));
            mTvFansNum.setText(String.valueOf(data.getFansNum()));
            if (data.getDynamicNum() == 0) {//如果没有动态，显示暂无动态
                mLlNoContent.setVisibility(View.VISIBLE);
            } else {
                mLlNoContent.setVisibility(View.GONE);
            }
            if ("女".equals(data.getSex())) {
                mIvGender.setImageResource(R.drawable.female);
            } else {
                mIvGender.setImageResource(R.drawable.male);
            }
        }
    }
}
